<?php
/**
 * 材料品类（大类）模型
 * (model)
 */
namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;

class ProductMaterialClassifyMax extends Model{

    protected $table = 'product_material_classify_max';

    protected $guarded = [];

    public function classify_min(){
        return $this->hasMany(ProductMaterialClassifyMin::class, 'parent_id', 'id');
    }

    public static function options($where = []){
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        $res = $query->get()->map(function ($model){
            return collect(['classify' => $model->title.'-'.$model->code,
                'id' => $model->id]);
        });
        return $res->pluck('classify','id');
    }

    public static function getModelById($id){
        return self::query()->find($id);
    }

    public static function getModelByCode($code){
        return self::query()->where('code',$code)->first();
    }

    public static function getValueByCode($code,$field){
        return self::query()->where('code',$code)->value($field);
    }

    public static function getValueById($id,$field){
        return self::query()->where('id',$id)->value($field);
    }

}
